﻿namespace LastVersionOfIpPackage.GUI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using LastVersionOfIpPackage.Enum_needed;
    using LastVersionOfIpPackage.ImageOperations;

    /// <summary>
    /// morphology form
    /// </summary>
    public partial class Morphological : Form
    {
        /// <summary>
        /// flag to known if it save in orginal or not
        /// </summary>
        private bool saveOrNot = false;

        /// <summary>
        /// result image
        /// </summary>
        private Bitmap resultImg;

        /// <summary>
        /// Initializes a new instance of the <see cref="Morphological"/> class.
        /// </summary>
        public Morphological()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Gets or sets a value indicating whether [save or not1].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [save or not1]; otherwise, <c>false</c>.
        /// </value>
        public bool SaveOrNot1
        {
            get { return this.saveOrNot; }
            set { this.saveOrNot = value; }
        }

        /// <summary>
        /// Gets or sets the result img.
        /// </summary>
        /// <value>
        /// The result img.
        /// </value>
        public Bitmap ResultImg
        {
            get { return this.resultImg; }
            set { this.resultImg = value; }
        }

        /// <summary>
        /// Sets the info of orignal image.
        /// </summary>
        /// <param name="image">The orginal image.</param>
        /// <param name="width">The width of orignal image.</param>
        /// <param name="height">The height of orignal image.</param>
        internal void SetInfo(Image image, string width, string height)
        {
            this.picOrg.Image = image;
            this.LabWdth.Text = width;
            this.LabHth.Text = height;
        }

        /// <summary>
        /// Handles the Click event of the Button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Button1_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();
            this.dataGridView1.ColumnCount = 0;
            this.dataGridView1.ColumnCount = int.Parse(this.txtWidth.Text);
            this.dataGridView1.Rows.Add(int.Parse(this.txtHeigth.Text));

            for (int j = 0; j < int.Parse(this.txtWidth.Text); j++)
            {
                for (int i = 0; i < int.Parse(this.txtHeigth.Text); i++)
                {
                    this.dataGridView1.Rows[i].Cells[j].Value = 1;
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the BtApply control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtApply_Click(object sender, EventArgs e)
        {
            try
            {
                Point original = new Point();
                original.X = int.Parse(this.txtOriginal.Text.Split(',')[0]);
                original.Y = int.Parse(this.txtOriginal.Text.Split(',')[1]);
                int[,] setB = new int[int.Parse(this.txtWidth.Text), int.Parse(this.txtHeigth.Text)];
                for (int i = 0; i < int.Parse(this.txtWidth.Text); i++)
                {
                    for (int j = 0; j < int.Parse(this.txtHeigth.Text); j++)
                    {
                        setB[i, j] = int.Parse(this.dataGridView1.Rows[j].Cells[i].Value.ToString()) * 255;
                    }
                }

                if (this.radDilation.Checked)
                {
                    this.resultImg = ImageEnchance.NewImageEnchanemwnt.Morphological((Bitmap)this.picOrg.Image, MorphologyType.Dilation, setB, 128, original);
                    this.picResultImage.Image = this.resultImg;
                }
                else if (this.radErosion.Checked)
                {
                    this.resultImg = ImageEnchance.NewImageEnchanemwnt.Morphological((Bitmap)this.picOrg.Image, MorphologyType.Erosion, setB, 128, original);
                    this.picResultImage.Image = this.resultImg;
                }
                else
                {
                    MessageBox.Show("choose type of morphological need apply it");
                }
            }
            catch
            {
                if (this.picOrg.Image == null)
                {
                    MessageBox.Show("No image to make morphology");
                }
                else if (this.txtOriginal.Text == string.Empty)
                {
                    MessageBox.Show("Insert original point");
                }
                else
                {
                    MessageBox.Show("Check for input if true , try in next time or return to manufactor");
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the BtSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtSave_Click(object sender, EventArgs e)
        {
            if (this.picResultImage.Image != null)
            {
                this.saveOrNot = true;
                this.Close();
            }
        }

        /// <summary>
        /// Handles the Click event of the BtCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtCancel_Click(object sender, EventArgs e)
        {
            this.saveOrNot = false;
            this.Close();
        }
    }
}
